package net.liteheaven.xblib.widget.reddot;

import android.content.Context;
import android.graphics.Color;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.graphics.drawable.shapes.Shape;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import net.liteheaven.xblib.common.ui.ViewHelper;
import net.liteheaven.xblib.common.ui.DimensionUtil;

/**
 * 红点展示帮助类
 * Created by liangy on 2016/12/30.
 */
public class RedDotHelper {

    /**
     * 配置
     */
    private IRedDotLpGenerator mLpGenerator;
    private boolean mShowNumber = true;

    public RedDotHelper() {
    }

    /**
     * 设置LayoutParams生成器
     * @param lpGenerator
     */
    public void setLpGenerator(IRedDotLpGenerator lpGenerator) {
        if (lpGenerator != null) {
            mLpGenerator = lpGenerator;
        }
    }

    /**
     * 是否显示数字
     * @param show
     */
    public void showNumber(boolean show){
        mShowNumber = show;
    }

    /**
     * 假如FrameLayout A中有一个TextView B，要在B右上角显示小红点，那parent就传A
     *
     * @param reference 参照物视图（哪个视图要带红点）
     * @param unread 未读数目
     */
    public void show(View reference, int unread) {
        if (reference == null) {
            return;
        }
        ViewGroup parent = (ViewGroup)reference.getParent();

        DotContainerView container = null;
        for (int i = 0; i < parent.getChildCount(); ++i) {
            View child = parent.getChildAt(i);
            if (child instanceof DotContainerView) {
                container = (DotContainerView) child;
            }
        }

        Context ctx = parent.getContext();

        int w, h;
        String text;
        if (!mShowNumber){
            w = DimensionUtil.dip2px(ctx, 10);
            h = w;
            text = "";
        }else{
            if (unread < 10){
                w = DimensionUtil.dip2px(ctx, 18);
                h = w;
                text = "" + unread;
            }else if (unread < 100){
                w = DimensionUtil.dip2px(ctx, 26);
                h = DimensionUtil.dip2px(ctx, 18);
                text = "" + unread;
            }else{
                w = DimensionUtil.dip2px(ctx, 26);
                h = DimensionUtil.dip2px(ctx, 18);
                text = "…";
            }
        }

        if (container == null){
            if (unread > 0){
                container = new DotContainerView(ctx);
                if (mLpGenerator != null){
                    parent.addView(container, mLpGenerator.getLayoutParam(parent, reference));
                }else{
                    parent.addView(container);
                }
                container.setUnread(text);
                container.setUnreadLayoutParams(w, h);
            }
        }else{
            if (unread <= 0){
                parent.removeView(container);
            }else{
                container.setUnread(text);
                container.setUnreadLayoutParams(w, h);
            }
        }
    }

    /**
     * 红点视图
     */
    private class DotContainerView extends FrameLayout {

        public TextView dotView;

        public DotContainerView(Context context) {
            super(context);
            init();
        }

        public DotContainerView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }

        public DotContainerView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }

        private void init() {
            dotView = (TextView) createDotView(getContext(), this);
        }

        private View createDotView(Context ctx, FrameLayout parent) {
            TextView dotView = new TextView(ctx);

            // 外部矩形弧度
            float[] outerR = new float[] { 8, 8, 8, 8, 8, 8, 8, 8 };
            // 内部矩形与外部矩形的距离
            RectF inset = new RectF(100, 100, 50, 50);
            // 内部矩形弧度
            float[] innerRadii = new float[] { 20, 20, 20, 20, 20, 20, 20, 20 };
            Shape shape = new RoundRectShape(outerR, inset, innerRadii);
            Drawable drawable = new ShapeDrawable(shape);
            ViewHelper.setBackgroundCompat(dotView, drawable);

            int size = DimensionUtil.dip2px(ctx, 12);
            LayoutParams lp = new LayoutParams(size, size);
            lp.gravity = Gravity.CENTER;
            parent.addView(dotView, lp);
            dotView.setTextColor(Color.WHITE);
            dotView.setTextSize(12);
            dotView.setGravity(Gravity.CENTER);
            return dotView;
        }

        public void setUnread(String unread) {
            dotView.setText("" + unread);
        }

        public void setUnreadLayoutParams(int w, int h){
            ViewGroup.LayoutParams lp = dotView.getLayoutParams();
            lp.width = w;
            lp.height = h;
            dotView.setLayoutParams(lp);
        }
    }

    /**
     * 红点布局参数生成接口
     */
    public interface IRedDotLpGenerator{
        ViewGroup.LayoutParams getLayoutParam(ViewGroup parent, View referenceView);
    }

    /**
     * 布局参数生成器构造
     */
    public static class LpGeneratorBuilder{
        public static IRedDotLpGenerator getDefaultFrameLayoutLpGenerator(){
            IRedDotLpGenerator iRedDotLpGenerator = new IRedDotLpGenerator(){

                @Override
                public ViewGroup.LayoutParams getLayoutParam(ViewGroup parent, View referenceView) {
                    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(-2, -2);
                    FrameLayout.LayoutParams lpReference = (FrameLayout.LayoutParams)referenceView.getLayoutParams();
                    // 往左下偏移2dp
                    int commonMargin = DimensionUtil.dip2px(parent.getContext(), 2);
                    referenceView.measure(0, 0);
                    int marginX = referenceView.getMeasuredWidth() / 2 + lpReference.leftMargin - commonMargin;
                    int marginY = - referenceView.getMeasuredHeight() / 2 + lpReference.topMargin + commonMargin;
                    lp.setMargins(marginX, marginY, 0, 0);
                    lp.gravity = Gravity.CENTER;
                    return lp;
                }
            };
            return iRedDotLpGenerator;
        }
    }
}